package com.wd.visitor.common.controller;

import com.wd.visitor.common.service.ImageService;
import com.wd.visitor.common.utils.ImageUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @className: ImageController
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/3
 **/

@RestController
public class ImageController {


    @Resource
    ImageService imageService;

    @GetMapping("/image/test")
    public String edgeImage(String file) {

        System.out.println(file);


        return "data:image/png;base64," + ImageUtils.loadBase64Image("downloaded_image.png", "/");
    }


    @GetMapping("/image/edge")
    public ResponseEntity<ByteArrayResource> getImage(String file) {
        try {
            byte[] imageBytes = imageService.downloadImage(file);

            ByteArrayResource resource = new ByteArrayResource(imageBytes);

            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=image.png");
            headers.setContentType(MediaType.IMAGE_PNG);

            return ResponseEntity.ok()
                    .headers(headers)
                    .body(resource);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }


}
